import {request} from '../../utils/request';
import {load} from '../../utils/Storage';
import Toast from '../../components/widget/Toast';
import Loading from '../../components/widget/Loading';
import {action, observable,makeAutoObservable} from 'mobx';
const SIZENum: number = 10;
 class HomeStore {
  page: number = 1;
  @observable homeList: ArticleSimple[] = [];
  @observable refreshing: boolean = false;
  @observable categoryList: Category[] = [];
  constructor() {
    makeAutoObservable(this);
    this.page = 1; // 初始化为 1
    this.homeList = [];
    this.refreshing = false;
    this.categoryList = [];
  }
  @action
  resetPage = () => {
    this.page = 1;
  };
  requestHomeList = async () => {
    if (this.refreshing) {
      return;
    }
    Loading.show();
    try {
      this.refreshing = true;
      const params = {
        page: this.page,
        size: SIZENum,
      };
      // Make a request to the 'homeList' endpoint with the given parameters
      // 请求参数
      const {data} = await request('homeList', params);
      console.log('返回多数23', data);
      if (data?.length) {
        if (this.page === 1) {
          this.homeList = data;
        } else {
          this.homeList = [...this.homeList, ...data];
        }
        this.page = this.page + 1;
      } else {
        if (this.page === 1) {
          this.homeList = [];
        } else {
        }
      }
    } catch (error) {
      console.log(error);
      Toast.show('网络错误，请重试！');
    } finally {
      // 停止刷新
      this.refreshing = false;
      Loading.hide();
    }
  };
  getCategoryList = async () => {
    // 加载缓存数据
    const cacheListStr = await load('categoryList');
    if (cacheListStr) {
      // 解析缓存数据
      const cacheList = JSON.parse(cacheListStr);
      console.log('缓存数据', cacheList);
      if (cacheList?.length) {
        // 如果缓存数据不为空，则赋值给categoryList
        this.categoryList = cacheList;
      } else {
        // 如果缓存数据为空，则使用默认分类列表
        this.categoryList = DEFAULT_CATEGORY_LIST;
      }
    } else {
      // 如果没有缓存数据，则使用默认分类列表
      this.categoryList = DEFAULT_CATEGORY_LIST;
    }
  };
}

const DEFAULT_CATEGORY_LIST: Category[] = [
  // 默认添加频道
  {name: '推荐', default: true, isAdd: true},
  {name: '视频', default: true, isAdd: true},
  {name: '直播', default: true, isAdd: true},
  {name: '摄影', default: false, isAdd: true},

  {name: '穿搭', default: false, isAdd: true},
  {name: '读书', default: false, isAdd: true},
  {name: '影视', default: false, isAdd: true},
  {name: '科技', default: false, isAdd: true},

  {name: '健身', default: false, isAdd: true},
  {name: '科普', default: false, isAdd: true},
  {name: '美食', default: false, isAdd: true},
  {name: '情感', default: false, isAdd: true},

  {name: '舞蹈', default: false, isAdd: true},
  {name: '学习', default: false, isAdd: true},
  {name: '男士', default: false, isAdd: true},
  {name: '搞笑', default: false, isAdd: true},

  {name: '汽车', default: false, isAdd: true},
  {name: '职场', default: false, isAdd: true},
  {name: '运动', default: false, isAdd: true},
  {name: '旅行', default: false, isAdd: true},

  {name: '音乐', default: false, isAdd: true},
  {name: '护肤', default: false, isAdd: true},
  {name: '动漫', default: false, isAdd: true},
  {name: '游戏', default: false, isAdd: true},

  // 默认添加频道
  {name: '家装', default: false, isAdd: false},
  {name: '心理', default: false, isAdd: false},
  {name: '户外', default: false, isAdd: false},
  {name: '手工', default: false, isAdd: false},

  {name: '减脂', default: false, isAdd: false},
  {name: '校园', default: false, isAdd: false},
  {name: '社科', default: false, isAdd: false},
  {name: '露营', default: false, isAdd: false},

  {name: '文化', default: false, isAdd: false},
  {name: '机车', default: false, isAdd: false},
  {name: '艺术', default: false, isAdd: false},
  {name: '婚姻', default: false, isAdd: false},

  {name: '家居', default: false, isAdd: false},
  {name: '母婴', default: false, isAdd: false},
  {name: '绘画', default: false, isAdd: false},
  {name: '壁纸', default: false, isAdd: false},

  {name: '头像', default: false, isAdd: false},
];
export default  new HomeStore()